package com.warm.core.utils;

import cn.hutool.core.util.ObjectUtil;
import com.warm.core.exception.JsonException;
import lombok.extern.slf4j.Slf4j;
import org.noear.snack.ONode;
import org.noear.snack.core.DEFAULTS;
import org.noear.snack.core.Feature;
import org.noear.snack.core.Options;

import java.util.List;

/**
 * json转换工具类
 *
 * @author warm
 */
@Slf4j
public class JsonUtil {

    public static int features_serialize = Feature.of(
            Feature.OrderedField,
            Feature.WriteDateUseTicks,
            Feature.WriteClassName,
            Feature.QuoteFieldNames);

    /**
     * 把Object转json字符串
     *
     * @param obj
     * @return
     */
    public static String objectToJson(Object obj) {
        if (ObjectUtil.isNull(obj)) {
            return null;
        }
        try {
            return ONode.load(obj, new Options(features_serialize)).toJson();
        } catch (Exception e) {
            log.error("把Object转json字符串失败！", e);
            throw new JsonException("把Object转json字符串失败！", e);
        }
    }

    /**
     * json字符串转换为对象
     *
     * @param json
     * @return
     */
    public static <T> T jsonToObject(String json) {
        if (StringUtils.isEmpty(json)) {
            return null;
        }
        try {
            return ONode.deserialize(json);
        } catch (Exception e) {
            log.error("json字符串转换为对象！", e);
            throw new JsonException("json字符串转换为对象！", e);
        }
    }

    /**
     * json字符串转换为对象
     *
     * @param json
     * @param obj
     * @return
     */
    public static <T> T jsonToObject(String json, Class<T> obj) {
        if (StringUtils.isEmpty(json)) {
            return null;
        }
        try {
            return ONode.deserialize(json, obj);
        } catch (Exception e) {
            log.error("json字符串转换为对象！", e);
            throw new JsonException("json字符串转换为对象！", e);
        }
    }

    /**
     * json字符串转换为list对象
     *
     * @param json
     * @return
     */
    public static <T> List<T> jsonToList(String json) {
        if (StringUtils.isEmpty(json)) {
            return null;
        }
        try {
            return ONode.deserialize(json);
        } catch (Exception e) {
            log.error("json字符串转换为list对象！", e);
            throw new JsonException("json字符串转换为list对象！", e);
        }
    }

    /**
     * json字符串转换为list对象
     *
     * @param json
     * @param obj
     * @return
     */
    public static <T> List<T> jsonToList(String json, Class<T> obj) {
        if (StringUtils.isEmpty(json)) {
            return null;
        }
        try {
            return ONode.deserialize(json, obj);
        } catch (Exception e) {
            log.error("json字符串转换为list对象！", e);
            throw new JsonException("json字符串转换为list对象！", e);
        }
    }

    public static String stringify(Object source, String[] filters) {
        return stringify(source, Options.def(), filters);
    }

    public static String stringify(Object source, Options opts, String[] filters) {
        ONode load = ONode.load(source, opts, DEFAULTS.DEF_OBJECT_FROMER);
        for (String filter : filters) {
            load.remove(filter);
        }
        return load.toString();
    }

}
